//
//  LoginViewController.m
//  penzer
//
//  Created by caiyc on 2018/9/18.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "HostLoginViewController.h"
#import "HostHomeViewController.h"
#import "AppDelegate.h"
#import <IDLFaceSDK/FaceSDKManager.h>
#import "LivenessViewController.h"
#import "LivingConfigModel.h"
#import "Host_IndexViewController.h"
#import "RegistFViewController.h"
#import "HostRegiViewController.h"
#import "DetectionViewController.h"
#import "BloginViewController.h"

//#import "RegistFViewController.h"
@interface HostLoginViewController ()<UITextFieldDelegate,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *balid_Btn;
@property(assign)BOOL validType;
- (IBAction)getValid:(UIButton *)sender;

@end

@implementation HostLoginViewController
- (IBAction)login:(UIButton *)sender {
    [self.view endEditing: 1];
    if([self.mob_Tf.text isEqualToString:@""]||[self.pass_Tf.text isEqualToString:@""]){
        [WToast showWithText:@"请填写完整的登录信息"];
        return;
    }
    NSDictionary *params = @{@"ctrl":@"live_anchor",@"ac":@"auth",@"op":@"login",@"mobile":self.mob_Tf.text,@"password":self.pass_Tf.text,@"ta":@"index"};
    [self weqPostHandle:params success:^(id response) {
        if(response){
            NSDictionary *userInfo = [NSDictionary changeType:response[@"data"]] ;
            [XTool SaveDefaultInfo:userInfo Key:HOST_USER];
            [XTool SaveDefaultInfo:@"1" Key:Prole];
            AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
            [app changeRoot:1];

            
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation:1 andTitle:@"" andNaviColor:nil andTitleColor:nil];
    self.balid_Btn.hidden = 1;
    self.mob_Tf.delegate = self;
    self.botom_ScroView.contentSize = CGSizeMake(screen_width, screen_height*1.2);
    self.botom_ScroView.delegate = self;
    self.botom_ScroView.showsVerticalScrollIndicator = 0;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKb)];
    [self.view addGestureRecognizer:tap];
    // Do any additional setup after loading the view from its nib.
}
-(void)hideKb{
    [self.view endEditing:1];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:1];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.mob_Tf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.mob_Tf.text.length >= 11) {
            self.mob_Tf.text = [textField.text substringToIndex:11];
            return NO;
        }
    }
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
//切换登录方式
- (IBAction)gotoregist:(UIButton *)sender {
    if(self.validType==0){
        self.validType = 1;
        self.valid_text.text = @"验证码登录";
        self.pass_Tf.placeholder = @"请输入验证码";
         [self.type_Btn setTitle:@"密码登录" forState:UIControlStateNormal];
        self.balid_Btn.hidden = 0;
    }else{
        self.validType = 0;
        self.valid_text.text = @"密码登录";
        [self.type_Btn setTitle:@"验证码登录" forState:UIControlStateNormal];
        self.pass_Tf.placeholder = @"输入密码，长度不小于6位";
        self.balid_Btn.hidden = 0;
    }
    
}
- (IBAction)getValid:(UIButton *)sender {
    
        if([self.mob_Tf.text isEqualToString:@""]){
            [WToast showWithText:@"请输入正确的手机号"];
            return;
        }
        NSString *md5Str = [NSString stringWithFormat:@"km%@bz",self.mob_Tf.text];
        NSString *tokens = [XTool md5:md5Str];
        NSDictionary *param = @{@"ctrl":@"common",@"mobile":self.mob_Tf.text,@"captcha":tokens,@"ac":@"code"};
        [self weqPostHandle:param success:^(id response) {
           
            if(response){
                [WToast showWithText:@"发送成功"];
            }
        } failure:^(NSError *error) {
            
        }];
  
}
//注册
- (IBAction)face_Login:(UIButton *)sender {
    LivenessViewController* lvc = [[LivenessViewController alloc] init];
    LivingConfigModel* model = [LivingConfigModel sharedInstance];
    NSArray *arr = @[@(FaceLivenessActionTypeLiveEye)];
    model.isByOrder = YES;
    [model.liveActionArray addObjectsFromArray:arr];
    [lvc livenesswithList:model.liveActionArray order:model.isByOrder numberOfLiveness:model.numOfLiveness];
    lvc.completion=^(NSDictionary *images, UIImage *originImage){
        if([images[@"bestImage"] count] > 0){
            NSData* data = [[NSData alloc] initWithBase64EncodedString:[images[@"bestImage"] lastObject] options:NSDataBase64DecodingIgnoreUnknownCharacters];
            NSString *base_Str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            //self.base_Str = base_Str;
            [self uploadFace:base_Str];
            NSLog(@"base-------%@",base_Str);
        }
    };
    [self pushSecondVC:lvc];
//    DetectionViewController* dvc = [[DetectionViewController alloc] init];
//    dvc.completion=^(NSDictionary *images, UIImage *originImage){
//        NSData* data = [[NSData alloc] initWithBase64EncodedString:[images[@"bestImage"] lastObject] options:NSDataBase64DecodingIgnoreUnknownCharacters];
//
//        NSString *base_Str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//        //self.base_Str = base_Str;
//        [self uploadFace:base_Str];
//        NSLog(@"base-------%@",base_Str);
//    };
//    [self pushSecondVC:dvc];
}
-(void)uploadFace:(NSString *)base64{
    NSDictionary *param = @{@"image":base64,@"ta":@"face_login",@"op":@"login",@"ctrl":@"live_anchor",@"ac":@"auth"};
    [self weqPostHandle:param success:^(id response) {
        if(response){
            
            [XTool SaveDefaultInfo:response[@"data"] Key:HOST_USER];
            [XTool SaveDefaultInfo:@"1" Key:Prole];
           [WToast showWithText:@"登录成功"];
            AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
            [app changeRoot:1];
         }
    } failure:^(NSError *error) {
        
    }];
}
- (IBAction)regist:(UIButton *)sender {
    [self.view endEditing:1];
    HostRegiViewController *vc = [[HostRegiViewController alloc]init];
    [self pushSecondVC:vc];
}
//忘记密码
- (IBAction)forgetPass:(UIButton *)sender {
    [self.view endEditing:1];
}
//显示密码
- (IBAction)showPassword:(UIButton *)sender {
    self.pass_Tf.secureTextEntry = 0;
}
@end
